package com.example.easyscript.controller.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.easyscript.entity.task.Operation;
import com.example.easyscript.entity.task.Step;
import com.example.easyscript.service.task.StepService;
import com.example.easyscript.service.task.TaskLoader;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author huzixian
 * @date 2023-03-08
 */
@Api(value = "StepController", tags = "")
@Slf4j
@RestController
@RequestMapping(value = "/web/step")
public class StepController {

    @Resource
    private StepService stepService;

    @ApiOperation("获取对应任务的状态集")
    @GetMapping("/getList/{operationId}")
    public List<Step> getList(@PathVariable Integer operationId) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("operation_id", operationId);
        return stepService.list(wrapper);
    }

    @ApiOperation("新增状态")
    @PostMapping("/save")
    public void save(@RequestBody Step step) {
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("operation_id", step.getOperationId());
        List list = stepService.list(wrapper);
        int order = list == null ? 0 : list.size();
        step.setOrderId(order + 1);
        stepService.save(step);
    }

    @ApiOperation("删除")
    @PostMapping("/delete/{id}")
    public void delete(@PathVariable Integer id) {
        stepService.removeById(id);
    }
}